package ph.com.gs3.formalistics.model.rdao;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;

import ph.com.gs3.formalistics.model.HttpCommunicator;
import ph.com.gs3.formalistics.model.RemoteDataAccessObject;
import ph.com.gs3.formalistics.model.vo.application.APIResponse;
import ph.com.gs3.formalistics.model.vo.application.APIResponse.APIInvalidResponseException;

public class CommentsRDAO extends RemoteDataAccessObject {

	public CommentsRDAO(String server) {
		super(new HttpCommunicator(), server);
	}

	public APIResponse getFormDocumentsCommentUpdates(String formWebId,
	        String[] documentWebIdArray, String lastUpdateDate, int rangeFrom, int rangeTo)
	        throws RDAOCommunicationException, APIInvalidResponseException {

		commonValidation();

		String url = server + "/API/form-requests-comments";

		JSONArray parsedDocumentWebIdList = new JSONArray();

		for (String documentWebId : documentWebIdArray) {
			parsedDocumentWebIdList.put(documentWebId);
		}

		Map<String, String> requestParams = new HashMap<>();

		requestParams.put("form_id", formWebId);
		requestParams.put("request_id_list", parsedDocumentWebIdList.toString());
		requestParams.put("range_from", Integer.toString(rangeFrom));
		requestParams.put("range_to", Integer.toString(rangeTo));

		if (lastUpdateDate != null) {
			requestParams.put("last_update_date", lastUpdateDate);
		}

		return getResponse(url, requestParams);

	}

	public APIResponse submitComment(String commentText, String formWebId, String requestWebId)
	        throws APIInvalidResponseException, RDAOCommunicationException {

		commonValidation();

		String url = server + "/API/write-comment";

		Map<String, String> requestParams = new HashMap<>();

		requestParams.put("comment", commentText);
		requestParams.put("form_id", formWebId);
		requestParams.put("request_id", requestWebId);

		return getResponse(url, requestParams);

	}

	public APIResponse deleteComment(String commentWebId) throws RDAOCommunicationException,
	        APIInvalidResponseException {

		commonValidation();

		String url = server + "/API/delete-comment";

		Map<String, String> requestParams = new HashMap<>();
		requestParams.put("comment_id", commentWebId);

		return getResponse(url, requestParams);
	}

	public APIResponse getDeletedComments(String lastUpdateDate) throws RDAOCommunicationException,
	        APIInvalidResponseException {

		commonValidation();

		String url = server + "/API/all-deleted-comments";

		Map<String, String> requestParams = new HashMap<>();
		requestParams.put("last_update_date", lastUpdateDate);

		return getResponse(url, requestParams);
	}

}
